package com.cqut.util;

import org.apache.logging.log4j.util.Strings;
import org.springframework.util.ClassUtils;

import java.io.File;
import java.util.Objects;

public class FileUtils {

    public static final String ROOT = "/";

    public static String getUploadFileDir(String userName) {
        return ClassUtils.getDefaultClassLoader().getResource("").getPath() + "static/file/" + userName;
    }


    public static String concatWithForwardSlash(String... arr) {

        StringBuilder res = new StringBuilder();

        for (int i = 0; i < arr.length; i++) {
            String str = arr[i];

            if (str.charAt(0) == '/') {
                str = str.substring(1);
            }

            res.append(str);

            if (i < arr.length - 1 && !str.endsWith("/") && !str.equals("")) {
                res.append("/");
            }
        }

        return res.toString();
    }

    public static boolean strictDelete(String path) {
        File io = new File(path);

        //io是否存在
        if (!io.exists()) {
            return false;
        }

        //判断是文件或文件夹
        if (io.isFile()) {
            return deleteFile(path);
        } else {
            return deleteDirectory(path);
        }
    }

    public static boolean deleteFile(String filePath) {
        File file = new File(filePath);

        //文件不存在或不是文件返回false
        if (!file.exists() || !file.isFile()) {
            return false;
        }

        return file.delete();
    }

    public static boolean deleteDirectory(String dirPath) {

        //参数校验
        if (Strings.isBlank(dirPath)) {
            return false;
        }

        File directory = new File(dirPath);

        //不是文件夹，或者不存在
        if (!directory.isDirectory() || !directory.exists()) {
            return false;
        }

        //获取文件夹理的文件列表
        File[] files = directory.listFiles();

        //错误标识
        boolean flag = true;

        //进行递归删除
        if (!Objects.isNull(files)) {

            for (File file : files) {

                boolean isSuccess;

                //是文件则直接删除，否则继续递归
                if (file.isFile()) {
                    isSuccess = deleteFile(file);
                } else {
                    isSuccess = deleteDirectory(file);
                }

                if (!isSuccess) {
                    flag = false;
                    break;
                }

            }

        }

        //flag为false代表着删除过程中出现了问题，则直接返回false
        if (!flag) {
            return false;
        }

        return directory.delete();
    }

    public static boolean strictDelete(File io) {
        if (Objects.isNull(io)) {
            return false;
        }
        return strictDelete(io.getAbsolutePath());
    }

    public static boolean deleteFile(File file) {
        if (Objects.isNull(file)) {
            return false;
        }
        return deleteFile(file.getAbsolutePath());
    }

    public static boolean deleteDirectory(File dir) {
        if (Objects.isNull(dir)) {
            return false;
        }
        return deleteDirectory(dir.getAbsolutePath());
    }

    /**
     * 传入一个路径,返回其相对用户路径下的父路径
     * @param username
     * @param path
     * @param name
     * @return
     */
    public static String getParenPath(String username,String path,String name){
        if (ArgUtils.isHaveBlankArgs(username,path,name)){
            return null;
        }

        if (!path.contains(username)){
            return null;
        }

        String relativePath = path.substring(path.indexOf(username)+username.length());

        return relativePath.substring(0,relativePath.lastIndexOf(name));
    }
}
